<?php

/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\model\system;

use app\model\BaseModel;

/**
 * 首页楼层
 */
class Floor extends BaseModel
{
	public function addFloorBlockList($data){
		$res = model("pc_floor_block")->addList($data);
		return $this->success($res);
	}

    public function getFloorList($siteId, $state = '', $page = 1)
    {
        $condition[] = ['site_id', '=', $siteId];
        if ($state != '') $condition[] = ['state', '=', $state];
        $field = ['f.id', 'f.index', 'f.title', 'f.block_id', 'f.site_id', 'f.create_time', 'f.state', 'b.name', 'f.config'];
        $alias = 'f';
        $join = [
            ['pc_floor_block b', 'b.id = f.block_id', 'inner']
        ];

        $list = model('site_floor')->pageList($condition, $field, '', $page, PAGE_LIST_ROWS, $alias, $join);
        return $this->success($list);
    }

    public function getList($siteId)
    {
        $condition[] = ['site_id', '=', $siteId];
        $field = ['f.id', 'f.index', 'f.title', 'f.block_id', 'f.site_id', 'f.state', 'b.name', 'f.config'];
        $alias = 'f';
        $join = [
            ['pc_floor_block b', 'b.id = f.block_id', 'inner']
        ];

        return model('site_floor')->getList($condition, $field, '', $alias, $join);
    }

    public function getDetail($floorId)
    {
        if (empty($floorId))
            return $this->error();

        $alias = 'a';
        $field = 'a.id,a.index,a.title,a.block_id,a.site_id,a.create_time,a.state,a.config,b.name,b.title block_title';
        $join = [
            ['pc_floor_block b', 'b.id=a.block_id', 'inner']
        ];

        $model = model('site_floor')->getInfo(['a.id' => $floorId], $field, $alias, $join);
        return $this->success($model);
    }

    public function addFloor($siteId, $title = '', $block_id = 0, $index = 0, $config = '')
    {
        if (empty($title)) {
            return $this->error('需要楼层标题');
        }
        if (empty($block_id)) {
            return $this->error('没有选择楼层模板');
        }

        $id = model('site_floor')->add([
            'index' => $index,
            'title' => $title,
            'block_id' => $block_id,
            'site_id' => $siteId,
            'create_time' => time(),
            'state' => 1,
            'config' => $config,
        ]);

        return $this->success($id);
    }

    public function editFloor($siteId, $floor_id, $title = '', $block_id = 0, $index = -1, $config = '', $state = 1)
    {
        if (empty($floor_id)) {
            return $this->error('没有指定楼层');
        }

        $floor = model('site_floor')->getInfo(['id' => $floor_id]);
        if (!empty($title)) $floor['title'] = $title;
        if (!empty($block_id)) $floor['block_id'] = $block_id;
        if ($index != -1) $floor['index'] = $index;
        if (!empty($config)) $floor['config'] = $config;
        $floor['state'] = $state;

        // if (empty($title)) {
        //     return $this->error('需要楼层标题');
        // }
        // if (empty($block_id)) {
        //     return $this->error('没有选择楼层模板');
        // }

        $id = model('site_floor')->update($floor, [['id', '=', $floor['id']]]);

        return $this->success();
    }

    public function delFloor($siteId, $floor_id)
    {
        if (empty($floor_id)) {
            return $this->error('没有指定楼层');
        }

        model('site_floor')->delete(['site_id' => $siteId, 'id' => $floor_id]);
        return $this->success();
    }
}
